﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Configuration;

namespace ZG_member
{
    public partial class GiftExBill : Form
    {
        MainForm mainform;
        public GiftExBill(MainForm main)
        {
            InitializeComponent();
            mainform = main;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"]; 
        }

        private void GiftExBill_Load(object sender, EventArgs e)
        {
            this.Opacity = 0;
            this.timer1.Start();
            CbxGiftsName.SelectedIndex = 0;
            giftExchangeNotes gifExNotes = new giftExchangeNotes();
            DataTable dt= gifExNotes.GetList("").Tables[0];
            DgvGiftNote.DataSource = dt;

            gifts gif = new gifts();
            DataTable dataTable = gif.GetList("").Tables[0];
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                if(!CbxGiftsName.Items.Contains(dataTable.Rows[i]["giftName"].ToString()))
                    CbxGiftsName.Items.Add(dataTable.Rows[i]["giftName"].ToString());
            }
            if (dt.Rows.Count > 0)
            {
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    memberInfo member = new memberInfo();
                //    Model.memberInfo memberMod = new Model.memberInfo();
                //    memberMod = member.GetModelList("memberId='" + DgvGiftNote.Rows[i].Cells["memberId"].Value.ToString() + "'")[0];
                //    DgvGiftNote.Rows[i].Cells["memberName"].Value = memberMod.memberName;
                //}
            }
        }

        private void btnInquiry_Click(object sender, EventArgs e)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (CbxGiftsName.Text != "所有礼品") 
            {
                stringBuilder.Append("giftName='" + CbxGiftsName.Text + "'");
                stringBuilder.Append("and ");
            } 
            if (!string.IsNullOrEmpty(TxtCardId.Text)) 
            {
                stringBuilder.Append("memberId='" + TxtCardId.Text.Trim() + "'");
                stringBuilder.Append("and ");
            }
            if (DateTime.Parse(DtpDate.Text) <= DateTime.Parse(DtpDate1.Text))
            {
                stringBuilder.Append("date between '" + DateTime.Parse(DtpDate.Text) + "'and'" + DateTime.Parse(DtpDate1.Text) + "'");
                stringBuilder.Append("and ");
            }
            else
            {
                stringBuilder.Append("date=''");
                stringBuilder.Append("and ");
                MessageBox.Show("日期错误");
            }
            string str = stringBuilder.ToString();
            if (str.Substring(str.Length - 4, 4) == "and ") 
            {
                str = str.Substring(0, str.Length - 4);
            }

            giftExchangeNotes gifExNotes = new giftExchangeNotes();
            DataTable dt = gifExNotes.GetList(str).Tables[0];
            DgvGiftNote.DataSource = dt;

            if (dt.Rows.Count > 0) 
            {
                //for (int i = 0; i < dt.Rows.Count; i++) 
                //{
                //    memberInfo member = new memberInfo();
                //    Model.memberInfo memberMod = new Model.memberInfo();
                //    memberMod = member.GetModelList("memberId='" + DgvGiftNote.Rows[i].Cells["memberId"].Value.ToString() + "'")[0];
                //    DgvGiftNote.Rows[i].Cells["memberName"].Value = memberMod.memberName;
                //}
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            ConsumptionBill consum = new ConsumptionBill(mainform);
            consum.ExportExcel("",DgvGiftNote);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }
        int flag = 0;
        private void GiftExBill_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();

            }
        }

        private void TxtCardId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                //if (e.KeyChar == '.')
                //{
                //    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                //        e.Handled = true;
                //}
                //else
                e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
                else if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                {
                    if (((TextBox)sender).Text.Trim().Substring(((TextBox)sender).Text.Trim().IndexOf('.') + 1).Length >= 4)
                        e.Handled = true;
                }
            }
        }

        

    }
}
